$(function() {
	$(".menu_right").bind("mouseenter", function() {
		$(this).css({
			"background-color" : "#F2F2F2"
		});
	});
	$(".menu_right").bind("mouseleave", function() {
		$(this).css({
			"background-color" : "#fff"
		});
	});
	//将窗口置于最上层
	window.nativeWindow.alwaysInFront = true;
	//失去焦点时关闭窗口
	window.nativeWindow.addEventListener(air.Event.DEACTIVATE, function() {
		window.nativeWindow.close();
	});
	//窗体始终在前	
	var configinfo = App.getConfig();
	if(configinfo.AlwaysInFront == 1) {
		$("#alwaysinfront").html("取消窗口始终在前");
		var win = Common.GetWin("C6 Air");
		if(win)
			win.alwaysInFront = true;
	} else {
		$("#alwaysinfront").html("窗口始终在前");
		var win = Common.GetWin("C6 Air");
		if(win)
			win.alwaysInFront = false;
	}
	
	//系统自动更新
	var appUpdater = null;
	appUpdater = new air.ApplicationUpdaterUI();
	appUpdater.configurationFile = new air.File("app:/config/update-config.xml");
	appUpdater.addEventListener(air.ErrorEvent.ERROR, function(event) {
		Common.ShowTips(event.toString());
	});
	appUpdater.addEventListener(air.UpdateEvent.BEFORE_INSTALL, function(event) {
		//安装前备份应用程序配置
		App.backupToXML();
	});
	appUpdater.initialize();

	$(".menu_right").bind("click", function() {
		switch($(this).attr("flag")) {
			case "diary":
				Common.OpenDiaryNew();
				break;
			case "alwaysinfront":
				//窗体始终在前
				if(configinfo.AlwaysInFront == 1) {
					configinfo.AlwaysInFront = 0;
					$(this).html("窗口始终在前");
					var win = Common.GetWin("C6 Air");
					if(win) {
						win.alwaysInFront = false;
					}

				} else {
					configinfo.AlwaysInFront = 1;
					$(this).html("取消窗口始终在前");
					var win = Common.GetWin("C6 Air");
					if(win) {
						win.alwaysInFront = true;
					}
				}
				App.setConfig(configinfo);
				break;
			case "checkupdate":
				appUpdater.checkNow();
				break;
			case "setting":
				Common.OpenSetWin();
				break;
			case "enterc6":
				//单点登录进入系统
				var config = App.getConfig();
				var c6url = config.WsURL.replace("JHSoft.Ws/jhwsforapp.asmx", "JHSoft.Web.ssoc6/LoginByURLNP.aspx?");
				c6url += base64encode("userid=" + config.LoginCode + "&password=" + config.Password);
				var urlReq = new air.URLRequest(c6url);
				air.navigateToURL(urlReq);
				break;
			case "changeuser":
				air.NativeApplication.nativeApplication.icon.bitmaps = [];
				Common.OpenLoginWin();
				var win = Common.GetWin("C6 Air");
				win.close();
				break;
			case "quit":
				appClose();
				break;
		}
		window.nativeWindow.close();
	});
});
//保存信息退出应用程序
function appClose() {
	var Config = Common.GetEmptyConfig();
	var win = Common.GetWin("C6 Air");
	if(!win)
		return;
	Config.WinStartX = win.x.toString();
	Config.WinStartY = win.y.toString();
	Config.WinWidth = win.width.toString();
	Config.WinHeight = window.returnValue.split("$#$")[0].trim();
	Config.CallNum = window.returnValue.split("$#$")[1].trim();
	App.setConfig(Config);
	air.NativeApplication.nativeApplication.icon.bitmaps = [];
	air.NativeApplication.nativeApplication.exit();
}